// jdbf.js - 音乐播放核心模块（适配Hiker框架）
class AudioSingle {
  constructor() {
    // 播放器核心状态
    this.player = null; // 音频实例
    this.currentSong = {}; // 当前歌曲信息
    this.completionCb = null; // 播放完成回调
    this.errorCb = null; // 播放错误回调
    this.id = ""; // 当前歌曲ID
  }

  /**
   * 初始化音频播放器
   * @returns {boolean} - 是否初始化成功
   */
  initPlayer() {
    if (this.player) return true;
    try {
      // 适配Hiker环境创建音频实例（若环境有默认音频API可替换）
      this.player = new Audio();
      // 绑定原生事件
      this.player.onended = () => this.completionCb && this.completionCb();
      this.player.onerror = () => this.errorCb && this.errorCb();
      return true;
    } catch (e) {
      console.error("播放器初始化失败：", e);
      return false;
    }
  }

  /**
   * 播放网络音频
   * @param {string} url - 音频播放地址
   * @param {Function} readyCb - 音频就绪回调
   * @param {Function} playCb - 播放开始回调
   */
  playNetwor(url, readyCb, playCb) {
    if (!this.initPlayer()) return;

    // 停止当前播放
    this.player.pause();
    this.player.src = url;

    // 音频加载就绪
    this.player.oncanplay = () => {
      readyCb && readyCb();
      // 开始播放
      this.player.play()
        .then(() => playCb && playCb())
        .catch(err => {
          console.error("播放失败：", err);
          this.errorCb && this.errorCb();
        });
    };
  }

  /**
   * 播放/暂停切换
   */
  startOrPause() {
    if (!this.player) return;
    this.player.paused ? this.player.play() : this.player.pause();
  }

  /**
   * 判断是否正在播放
   * @returns {boolean} - 播放状态
   */
  isPlaying() {
    return this.player && !this.player.paused;
  }

  /**
   * 判断播放器是否已创建
   * @returns {boolean} - 播放器存在状态
   */
  hasPlayer() {
    return !!this.player;
  }

  /**
   * 设置播放完成回调
   * @param {Function} cb - 回调函数
   */
  setCompletionListener(cb) {
    this.completionCb = cb;
  }

  /**
   * 设置播放错误回调
   * @param {Function} cb - 回调函数
   */
  setErrorListener(cb) {
    this.errorCb = cb;
  }

  /**
   * 从预设榜单随机获取歌曲（模拟接口逻辑，实际需对接真实音乐API）
   * @param {string} mode - 榜单类型（热歌榜/新歌榜等）
   * @returns {Object} - 歌曲信息（id/name/artistsname/picurl/url）
   */
  getRandomSong(mode) {
    // 模拟不同榜单的歌曲数据（实际项目需替换为真实API请求）
    const mockSongs = {
      "热歌榜": [
        { id: "1001", name: "热歌示例1", artistsname: "歌手A", picurl: "https://example.com/pic1.jpg", url: "https://example.com/audio1.mp3" },
        { id: "1002", name: "热歌示例2", artistsname: "歌手B", picurl: "https://example.com/pic2.jpg", url: "https://example.com/audio2.mp3" }
      ],
      "新歌榜": [
        { id: "2001", name: "新歌示例1", artistsname: "歌手C", picurl: "https://example.com/pic3.jpg", url: "https://example.com/audio3.mp3" },
        { id: "2002", name: "新歌示例2", artistsname: "歌手D", picurl: "https://example.com/pic4.jpg", url: "https://example.com/audio4.mp3" }
      ],
      // 其他榜单（飙升榜/抖音榜/电音榜）同理补充
      "飙升榜": [{ id: "3001", name: "飙升示例1", artistsname: "歌手E", picurl: "https://example.com/pic5.jpg", url: "https://example.com/audio5.mp3" }],
      "抖音榜": [{ id: "4001", name: "抖音示例1", artistsname: "歌手F", picurl: "https://example.com/pic6.jpg", url: "https://example.com/audio6.mp3" }],
      "电音榜": [{ id: "5001", name: "电音示例1", artistsname: "歌手G", picurl: "https://example.com/pic7.jpg", url: "https://example.com/audio7.mp3" }]
    };

    // 随机返回榜单中的一首歌曲
    const songs = mockSongs[mode] || mockSongs["热歌榜"];
    this.currentSong = songs[Math.floor(Math.random() * songs.length)];
    this.id = this.currentSong.id;
    return this.currentSong;
  }

  /**
   * 从自定义歌单ID获取随机歌曲（模拟逻辑）
   * @param {string} listId - 自定义歌单ID
   * @returns {Object} - 歌曲信息
   */
  getRandomListSong(listId) {
    // 模拟自定义歌单数据（实际需通过listId调用API获取）
    const customSongs = [
      { id: `list_${listId}_1`, name: `自定义歌单曲1`, artistsname: "自定义歌手1", picurl: "https://example.com/custom1.jpg", url: `https://example.com/custom_audio1.mp3` },
      { id: `list_${listId}_2`, name: `自定义歌单曲2`, artistsname: "自定义歌手2", picurl: "https://example.com/custom2.jpg", url: `https://example.com/custom_audio2.mp3` }
    ];

    this.currentSong = customSongs[Math.floor(Math.random() * customSongs.length)];
    this.id = this.currentSong.id;
    return this.currentSong;
  }
}

// 适配Hiker框架的模块导出（供GlobalVar.js调用）
if (typeof module !== "undefined" && module.exports) {
  module.exports = new AudioSingle();
} else if (typeof window !== "undefined") {
  window.AudioSingle = new AudioSingle();
}
